home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume8 / textools2 / part01 next >
Encoding:
Internet Message Format  |  1987-02-12  |  43.0 KB

  1. Subject:  v08i061:  A collection of tools for TeX users, Part01/02
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: Kamal Al-Yahya <kamal@hanauma.STANFORD.EDU>
  6. Mod.sources: Volume 8, Issue 61
  7. Archive-name: tektools2/Part01
  8.  
  9. #! /bin/sh
  10. # This is a shell archive, meaning:
  11. # 1. Remove everything above the #! /bin/sh line.
  12. # 2. Save the resulting text in a file.
  13. # 3. Execute the file with /bin/sh (not csh) to create the files:
  14. #    DeTeX.c
  15. #    Eqn.c
  16. #    Expand.c
  17. #    Match.c
  18. #    README
  19. #    TEX
  20. #    TEX.1
  21. #    detex.1
  22. #    detex1.c
  23. #    detex2.c
  24. #    inc_file
  25. #    inc_file2.tex
  26. #    makefile
  27. #    makefile.msc
  28. #    makefile.par
  29. #    setups.h
  30. #    subs.c
  31. #    testfile
  32. #    texeqn.1
  33. #    texeqn1.c
  34. #    texeqn2.c
  35. #    texexpand.1
  36. #    texexpand1.c
  37. #    texexpand2.c
  38. #    texmatch.1
  39. #    texmatch1.c
  40. #    texmatch2.c
  41. #    texspell
  42. #    texspell.1
  43. # This archive created: Thu Feb 12 10:14:11 1987
  44. export PATH; PATH=/bin:$PATH
  45. if test -f 'DeTeX.c'
  46. then
  47.     echo shar: will not over-write existing file "'DeTeX.c'"
  48. else
  49. cat << \SHAR_EOF > 'DeTeX.c'
  50. /* COPYRIGHT (C) 1987  Kamal Al-Yahya */
  51. #include   "setups.h"
  52. DeTeX(buffer,out_file)            /* stripping TEX commands */
  53.  
  54. char *buffer;
  55. FILE *out_file;
  56. {
  57. int c,cc;
  58. char w[MAXWORD];
  59.  
  60. while ((c = *buffer++) != NULL)
  61.     {
  62.     switch (c)
  63.         {
  64. /* detect TeX commands (backslash) */
  65.         case '\\':
  66.             c=' ' ;        /* "erase" the backslash */
  67.             putc(c,out_file);
  68.             cc = *buffer++;
  69.             if (cc == '\n')            putc(cc,out_file);
  70.             else if (cc == '[')        buffer += display(buffer);
  71.             else if (cc == '(')        buffer += formula(buffer);
  72.             else if (cc == '$' || cc == '%')
  73.                 break;
  74. /* check for LaTeX \begin{equation}, \begin{eqnarray}, and \begin{displaymath} */
  75.             else
  76.                 {
  77.                 buffer--;
  78.                 buffer += get_buf_word(buffer,w);
  79.                 if (strcmp(w,"begin") == 0)
  80.                     {
  81.                     buffer++;
  82.                     buffer += get_buf_word(buffer,w);
  83.                     if (strcmp(w,"equation") == 0 ||
  84.                         strcmp(w,"eqnarray") == 0 ||
  85.                         strcmp(w,"displaymath") == 0)
  86.                         buffer += begin_to_end(buffer,w);
  87.                     }
  88.                 }
  89.             break;
  90.  
  91.         case '$':
  92.             buffer += dollar(buffer,out_file);
  93.             break;
  94.         case '%':
  95.             buffer += comment(buffer);
  96.             break;
  97. /* erase these character */
  98.         case '{':
  99.             c=' ';
  100.         case '}':
  101.             c=' ';
  102.         case '_':
  103.             c=' ';
  104.         case '^':
  105.             c=' ';
  106.         case '&':
  107.             c=' ';
  108.         case '#':
  109.             c=' ';
  110. /* default is doing nothing: pass the character to the output */
  111.         default:
  112.             putc(c,out_file);
  113.             break;
  114.         }
  115.     }
  116. }
  117. SHAR_EOF
  118. fi # end of overwriting check
  119. if test -f 'Eqn.c'
  120. then
  121.     echo shar: will not over-write existing file "'Eqn.c'"
  122. else
  123. cat << \SHAR_EOF > 'Eqn.c'
  124. /* COPYRIGHT (C) 1987  Kamal Al-Yahya */
  125. #include    "setups.h"
  126. Eqn(buffer,out_file)            /* srips TEX equations */
  127.  
  128. FILE *out_file;
  129. char *buffer;
  130. {
  131. int c,d;
  132. int i;
  133. char w[MAXLINE], ww[MAXWORD];
  134. while ((c = *buffer++) != NULL)
  135.     {
  136.     if(c == '%')
  137.         {
  138.         while ((c = *buffer++) != NULL)
  139.             if (c == '\n') break;
  140.         }
  141.     else if(c == '$')
  142.         {
  143.         if ((d = *buffer++) == '$')
  144.             {
  145.             putc(c,out_file);    putc(d,out_file);
  146.             while ((c = *buffer++) != NULL)
  147.                 {
  148.                 if(c != '$')   putc(c,out_file);
  149.                 else
  150.                     {
  151.                     buffer++;
  152.                     fprintf(out_file,"$$ \n");
  153.                     break;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158. /* check for LaTeX \begin{equation}, \begin{eqnarray}, and \begin{displaymath} */
  159.     else if(c == '\\')
  160.         {
  161.         c = *buffer++;
  162.         if (c == '[')
  163.             {
  164.             putc('\\',out_file); putc(c,out_file);
  165.             while((c = *buffer++) != NULL)
  166.                 {
  167.                 if(c == '\\')
  168.                     {
  169.                     c = *buffer++;
  170.                     fprintf(out_file,"\\%c",c);
  171.                     if (c == ']')
  172.                         {
  173.                         putc('\n',out_file);
  174.                         break;
  175.                         }
  176.                     }
  177.                 else
  178.                     putc(c,out_file);
  179.                 }
  180.             continue;
  181.             }
  182.         buffer--;
  183.         buffer += get_buf_word(buffer,w);
  184.         if (strcmp(w,"begin") == 0)
  185.             {
  186.             buffer++;
  187.             i = get_buf_word(buffer,w);
  188.             buffer += i;
  189.             if (strcmp(w,"equation") == 0 || strcmp(w,"eqnarray")
  190.                 == 0 || strcmp(w,"displaymath") == 0)
  191.                 {
  192.                 fprintf(out_file,"\\begin{%s}",w);
  193.                 buffer++;
  194.                 while ((c = *buffer++) != NULL)
  195.                     {
  196.                     putc(c,out_file);
  197.                     if (c == '\\')
  198.                         {
  199.                         i = get_buf_word(buffer,ww);
  200.                         buffer += i;
  201.                         fprintf(out_file,"%s",ww);
  202.                         if (strcmp(ww,"end") == 0)
  203.                             {
  204.                             buffer++;
  205.                             i = get_buf_word(buffer,ww);
  206.                             buffer += i;
  207.                             fprintf(out_file,
  208.                                 "{%s}\n",ww);
  209.                             buffer++;
  210.                             if (strcmp(ww,"equation")
  211.                                 == 0 ||
  212.                                 strcmp(ww,"eqnarray")
  213.                                 == 0 ||
  214.                                 strcmp(ww,"displaymath")
  215.                                 == 0)
  216.                                 break;
  217.                             }
  218.                         }
  219.                     }
  220.                 }
  221.             }
  222.         else if (strcmp(w,"def") == 0)
  223.             {
  224.             i = def(buffer,w);
  225.             buffer += i;
  226.             fprintf(out_file,"\\def%s\n",w);
  227.             }
  228.         else if (strcmp(w,"newcommand") == 0)
  229.             {
  230.             i = command(buffer,w);
  231.             buffer += i;
  232.             fprintf(out_file,"\\newcommand%s\n",w);
  233.             }
  234.         }
  235.     }
  236. }
  237. SHAR_EOF
  238. fi # end of overwriting check
  239. if test -f 'Expand.c'
  240. then
  241.     echo shar: will not over-write existing file "'Expand.c'"
  242. else
  243. cat << \SHAR_EOF > 'Expand.c'
  244. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  245.  
  246. #include    "setups.h"
  247. unsigned int len=0;        /* length of document */
  248.  
  249. Expand(fp,buf)    /* expand TeX and LaTeX's \input and \include */
  250.  
  251. FILE *fp;
  252. char *buf;
  253. {
  254. char *buf2;
  255. FILE *fpp;
  256. int c;
  257. int c1=' ';                /* previous character */
  258. char w[MAXWORD];
  259. int i,j;
  260. extern wflag;
  261.  
  262. if (((buf2 = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL))
  263.     {
  264.         fprintf(stderr,"Expand: Cannot malloc() internal buffer space\n\
  265. Need an arrays of %d characters\n",MAXLEN);
  266.     exit(-1);
  267.     }
  268.  
  269. while ((c = getc(fp)) != EOF)
  270.     {
  271.     if (++len >= MAXLEN)
  272.         {
  273.         fprintf(stderr,"Document is too large\n");
  274.         exit(-1);
  275.         }
  276.     if (c == '%' || c1 == '%')
  277.         {
  278.         *buf++ = c;
  279.         while ((c =getc(fp)) != EOF)
  280.             {
  281.             if (++len >= MAXLEN)
  282.                 {
  283.                 fprintf(stderr,"Sorry: document is too large\n");
  284.                 exit(-1);
  285.                 }
  286.             *buf++=c;
  287.             if (c == '\n')        break;
  288.             }
  289.         c1=c;
  290.         continue;
  291.         }
  292.     if (c != '\\')
  293.         *buf++ = c;
  294.     else            /* detect TeX commands (backslash) */
  295.         {
  296.         /* see if \input or \include is the control sequence */
  297.         i=0;
  298.         c1=c;        /* update last character */
  299.         while ((c = getc(fp)) != EOF && i < MAXWORD)
  300.             {
  301.             if (++len >= MAXLEN)
  302.                 {
  303.                 fprintf(stderr,"Document is too large\n");
  304.                 exit(-1);
  305.                 }
  306.             if (c == ' ' || c=='\n' || c=='$' || c=='#' || c=='%'
  307.                 || c=='{' || c=='(' || c==')' || c == '\\')
  308.                 break;
  309.             w[i++] = (char)c;
  310.             }
  311.         if (strncmp(w,"input",5) == 0 || (strncmp(w,"include",7) == 0
  312.             && strcmp(w,"includeonly") !=0))
  313.             {
  314. /* if it is \input or \include , get the file name */
  315.             i=0;
  316.             while ((c=getc(fp)) != EOF && i < MAXWORD)
  317.                 {
  318.                 if (c == ' ' || c == '\n'
  319.                     || c == '\t' || c == '}' || c == '%')
  320.                     break;
  321.                 w[i++] = (char)c;
  322.                 }
  323.             w[i] = NULL;
  324.             fpp=fopen(w, "r"); /* open the new file */
  325.             if( fpp == NULL )
  326.                 {
  327. /* if file is not found, try file.tex  */
  328.                 strcat(w,".tex");
  329.                 fpp=fopen(w, "r");
  330.                 if( fpp == NULL )
  331.                     {
  332.                     fprintf(stderr,
  333.                     "TeXExpand: Cannot open %s\n",w);
  334.                     buf2[0] = NULL;
  335.                     }
  336.                 else
  337.                     {
  338.                     if (wflag != 1)
  339.                         {
  340.                         fprintf(stderr,"%s:\n",w);
  341.                         Match(fpp);
  342.                         fprintf(stderr,"\n");
  343.                         fseek(fpp,0,0);
  344.                         }
  345.                     Expand(fpp,buf2);
  346.                     fclose(fpp);
  347.                     }
  348.                 }
  349.             else
  350.                 {
  351.                 if (wflag != 1)
  352.                     {
  353.                     fprintf(stderr,"%s:\n",w);
  354.                     Match(fpp);
  355.                     fprintf(stderr,"\n");
  356.                     fseek(fpp,0,0);
  357.                     }
  358.                 Expand(fpp,buf2);
  359.                 fclose(fpp);
  360.                 }
  361.             strcat(buf,buf2);
  362.             buf += strlen(buf2);
  363.             w[0] = NULL;
  364.             }
  365.         else
  366. /* if the control sequence is not \input or \include write it out */
  367.             {
  368. /* if it is \def, \newcommand, or \newenvironment, write the full command */
  369.             if (strncmp(w,"def",3) == 0)
  370.                 {
  371.                 i = def_file(fp,&j,0);
  372.                 fseek(fp,-i,1);
  373.                 strcat(buf,"\\def\\");
  374.                 buf += 5;
  375.                 for (j=0; j < i; j++)
  376.                     *buf++=getc(fp);
  377.                 }
  378.             else if (strncmp(w,"newcommand",10) == 0)
  379.                 {
  380.                 i = comm_file(fp,&j,0);
  381.                 fseek(fp,-i,1);
  382.                 strcat(buf,"\\newcommand{");
  383.                 buf += 12;
  384.                 for (j=0; j < i; j++)
  385.                     *buf++=getc(fp);
  386.                 }
  387.             else if (strncmp(w,"newenvironment",14)==0)
  388.                 {
  389.                 i = getenv_file(fp,&j,0);
  390.                 fseek(fp,-i,1);
  391.                 strcat(buf,"\\newenvironment{");
  392.                 buf += 16;
  393.                 for (j=0; j < i; j++)
  394.                     *buf++=getc(fp);
  395.                 }
  396.             else
  397.                 {
  398.                 *buf++='\\';
  399.                 for (j=0; j < i; j++)
  400.                     *buf++ = w[j];
  401.                 *buf++ = c;
  402.                 }
  403.             }
  404.         }
  405.     c1 = c;                /* update last character */
  406.     }
  407. *buf = NULL;                /* terminate it with a null */
  408. }
  409. SHAR_EOF
  410. fi # end of overwriting check
  411. if test -f 'Match.c'
  412. then
  413.     echo shar: will not over-write existing file "'Match.c'"
  414. else
  415. cat << \SHAR_EOF > 'Match.c'
  416. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  417. #define IN_TM
  418. #include   "setups.h"
  419.  
  420. Match(fp)            /* check matching */
  421. FILE *fp;
  422. {
  423.  
  424. int line=1;            /* line counter */
  425. int ld=0;            /* single left dollar signs */
  426. int rd=0;            /* single right dollar signs */
  427. int ldd=0;            /* left double dollar signs */
  428. int rdd=0;            /* right double dollar signs */
  429. int disp=0;            /* \[  \] */
  430. int disp_line=1;        /* line number of \[ */
  431. int form=0;            /* \(  \) */
  432. int lform=1;            /* line number of \( */
  433. int lp=0;            /* left parenthesis */
  434. int rp=0;            /* right parenthesis */
  435. int lb=0;            /* left brackets */
  436. int rb=0;            /* right brackets */
  437. int lbr=0;            /* left braces */
  438. int rbr=0;            /* right braces */
  439. int c=' ';            /* current character */
  440. int c1=' ';            /* previous character */
  441. int lbrl=0;            /* line number of left braces */
  442. int lbl=0;            /* line number of left bracket */
  443. int lpl=0;            /* line number of left parenthesis */
  444. int ldl=1;            /* line number of left single dollar sign */
  445. int lddl=1;            /* line number of left double dollar sign */
  446. int warn=0;            /* warning status */
  447. int env_count = 0;        /* environment counter */
  448. int i=0, j=0;
  449. char w[MAXWORD];
  450. char *p;
  451. int cc;
  452. extern char *malloc();
  453.  
  454. while ((c =getc(fp)) != EOF)
  455.     {
  456.     if (ldd == 1 && ld == 1 && c != '$')
  457.         {
  458.         fprintf(stderr,"line %d: a double dollar sign is closed by a single dollar sign\n",line);
  459.         ld=0.;    ldd=0.;            /* reset dollar signs */
  460. /* Give warning about unclosed openings */
  461.         if ((lbr-rbr) > 0)
  462.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
  463.         if ((lb-rb) > 0)
  464.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
  465.         if ((lp-rp) > 0)
  466.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",lddl,lp-rp);
  467. /* clear registers */
  468.         lp=0; lb=0; lbr=0;
  469.         rp=0; rb=0; rbr=0;
  470.         lpl=0; lbrl=0; lbl=0;
  471.         }
  472.     switch(c)
  473.         {
  474.         case '\n':
  475.             line++;        /* increment line counter */
  476. /* check to see if a single dollar sign is not closed at the same line */
  477.             if (ld == 1 && warn == 0)
  478.                 {
  479.                 fprintf(stderr,"line %d: single dollar sign is not closed on the same line\n",line-1);
  480.                 warn=1;        /* warning has been given */
  481.                 }
  482.             break;
  483.         case '%':        /* ignore commented text */
  484.             while ((c =getc(fp)) != EOF)
  485.                 if (c == '\n')    {line++;    break;}
  486.             break;
  487.         case '{':
  488.             if (lbrl == 0)    lbrl=line;
  489.             lbr++;
  490.             break;
  491.         case '}':
  492.             rbr++;
  493.             if (rbr > lbr)
  494.                 {
  495.                 fprintf(stderr,"line %d: unmatched brace\n",line);
  496.                 rbr--;        /* reset counter */
  497.                 }
  498.             if (lbr == rbr)    lbrl=0;
  499.             break;
  500.         case '[':
  501.             if (lbl == 0)    lbl=line;
  502.             lb++;
  503.             break;
  504.         case ']':
  505.             rb++;
  506.             if (rb > lb)
  507.                 {
  508.                  fprintf(stderr,"line %d: unmatched bracket\n",line);
  509.                 rb--;        /* reset counter */
  510.                 }
  511.             if (lb == rb)    lbl=0;
  512.             break;
  513.         case '(':
  514.             if (lpl == 0)    lpl=line;
  515.             lp++;
  516.             break;
  517.         case ')':
  518.             rp++;
  519.             if (rp > lp)
  520.                 {
  521.                fprintf(stderr,"line %d: unmatched parenthesis\n",line);
  522.                 rp--;        /* reset counter */
  523.                 }
  524.             if (lp == rp)    lpl=0;
  525.             break;
  526.         case '$':
  527.             if (c1 == '$')        /* double dollar sign */
  528.                 {
  529.                 if (ld == 0)
  530.                     {
  531.                     fprintf(stderr,"line %d: single dollar sign is closed by a duble dollar sign\n",line);
  532.                     c=' ';        /* reset the dollar sign */
  533.                     break;
  534.                     }
  535.                 if (ldd == 1)
  536.                     {
  537.                     rdd=1; /* right double dollar sign */
  538. /* Give warning about unclosed openings */
  539.                     if ((lbr-rbr) > 0)
  540.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
  541.                     if ((lb-rb) > 0)
  542.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
  543.                     if ((lp-rp) > 0)
  544.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",lddl,lp-rp);
  545. /* clear registers */
  546.                     lp=0; lb=0; lbr=0;
  547.                     rp=0; rb=0; rbr=0;
  548.                     lpl=0; lbrl=0; lbl=0;
  549.                     }
  550.                 else
  551.                     {
  552.                     ldd=1;    /* left double dollar sign */
  553.                     lddl=line;    /* line number */
  554. /* Give warning about unclosed openings */
  555.                     if ((lbr-rbr) > 0)
  556.     fprintf(stderr,"line %d: %d unclosed braces before equation, first opened at line %d\n",lddl,lbr-rbr,lbrl);
  557.                     if ((lb-rb) > 0)
  558.     fprintf(stderr,"line %d: %d unclosed brackets before equation, first opened at line %d\n",lddl,lb-rb,lbl);
  559.                     if ((lp-rp) > 0)
  560.     fprintf(stderr,"line %d: %d unclosed parentheses before equation, first opened at line %d\n",lddl,lp-rp,lpl);
  561. /* clear registers */
  562.                     lp=0; lb=0; lbr=0;
  563.                     rp=0; rb=0; rbr=0;
  564.                     lpl=0; lbrl=0; lbl=0;
  565.                     }
  566.                 }
  567.             if (ld == 1)    rd=1;    /* right dollar sign */
  568.             else
  569.                 {
  570.                 ld=1;     /* left dollar sign */
  571.                 ldl=line;    /* line number */
  572.                 warn=0;    /* no warning has been given */
  573.                 }
  574.             break;
  575.         case '\\':
  576. /* check for \begin{...} and \end{...} */
  577.             i = get_file_word(fp,w,&line,&cc);
  578.             if (i == 0 && cc == '[')
  579.                 {
  580.                 if (disp == 1)
  581.     fprintf(stderr,"line %d: displayed equation starts, previous one at line %d not closed\n",line,disp_line);
  582.                 disp=1;     /* left   \] */
  583.                 disp_line=line;
  584. /* Give warning about unclosed openings */
  585.                 if ((lbr-rbr) > 0)
  586.     fprintf(stderr,"line %d: %d unclosed braces before equation\n",line,lbr-rbr);
  587.                 if ((lb-rb) > 0)
  588.     fprintf(stderr,"line %d: %d unclosed brackets before equation\n",line,lb-rb);
  589.                 if ((lp-rp) > 0)
  590.     fprintf(stderr,"line %d: %d unclosed parentheses before equation\n",line,lp-rp);
  591. /* clear registers */
  592.                 lp=0; lb=0; lbr=0;
  593.                 rp=0; rb=0; rbr=0;
  594.                 lpl=0; lbrl=0; lbl=0;
  595.                 }
  596.             else if (i == 0 && cc == ']')
  597.                 {
  598.                 if (disp == 0)
  599.     fprintf(stderr,"line %d: displayed equation ends but no beginning\n",line);
  600.                 disp=0;        /* right \] */
  601. /* Give warning about unclosed openings */
  602.                 if ((lbr-rbr) > 0)
  603.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",line,lbr-rbr);
  604.                 if ((lb-rb) > 0)
  605.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",line,lb-rb);
  606.                 if ((lp-rp) > 0)
  607.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",line,lp-rp);
  608. /* clear registers */
  609.                 lp=0; lb=0; lbr=0;
  610.                 rp=0; rb=0; rbr=0;
  611.                 lpl=0; lbrl=0; lbl=0;
  612.                 }
  613.             else if (i == 0 && cc == '(')
  614.                 {
  615.                 if (form == 1)
  616.     fprintf(stderr,"line %d: formula starts but previous one not closed\n",line);
  617.                 form=1;            /* left \( */
  618.                 lform=line;        /* line of \( */
  619.                 }
  620.             else if (i == 0 && cc == ')')
  621.                 {
  622.                 if (form == 0)
  623.     fprintf(stderr,"line %d: formula ends but no beginning\n",line);
  624.                 form=0;            /* right \) */
  625.                 }
  626.             else if (strcmp(w,"begin") == 0)
  627.                 {
  628.                 (void) get_file_word(fp,w,&line,&cc);
  629.                 if ((j=is_new_env(w,env_count)) < 0)
  630.                     {
  631.                     j = env_count;
  632.                     env[j].env_beg = 0;
  633.                     env[j].env_end = 0;
  634.                     p = (char *) malloc((unsigned)(i*sizeof(char)));
  635.                     strcpy(p,w);
  636.                     env[env_count++].env_name = p;
  637.                     }
  638.                 env[j].beg_line = line;
  639.                 env[j].env_beg++;
  640. /* Give warning about unclosed openings before these environments */
  641.                 if (strcmp(env[j].env_name,"equation") == 0 ||
  642.                     strcmp(env[j].env_name,"eqnarray") == 0 ||
  643.                     strcmp(env[j].env_name,"eqnarray*") == 0 ||
  644.                     strcmp(env[j].env_name,"displaymath") == 0)
  645.                     {
  646.                     if ((lbr-rbr) > 0)
  647.     fprintf(stderr,"line %d: %d unclosed braces before environment ``%s'', first opened at line %d\n",line,lbr-rbr,env[j].env_name,lbrl);
  648.                     if ((lb-rb) > 0)
  649.     fprintf(stderr,"line %d: %d unclosed brackets before environment ``%s'', first opened at line %d\n",line,lb-rb,env[j].env_name,lbl);
  650.                     if ((lp-rp) > 0)
  651.     fprintf(stderr,"line %d: %d unclosed parentheses before environment ``%s'', first opened at line %d\n",line,lp-rp,env[j].env_name,lpl);
  652. /* clear registers */
  653.                     lp=0; lb=0; lbr=0;
  654.                     rp=0; rb=0; rbr=0;
  655.                     lpl=0; lbrl=0; lbl=0;
  656.                     }
  657.                 }
  658.             else if (strcmp(w,"end") == 0)
  659.                 {
  660.                 (void) get_file_word(fp,w,&line,&cc);
  661.                 if ((j=is_new_env(w,env_count)) < 0)
  662.     fprintf(stderr,"line %d: unmatched end for environment ``%s''\n",line,w);
  663.                 else
  664.                 {
  665.                 env[j].env_end++;
  666.                 if (env[j].env_end > env[j].env_beg)
  667.                     {
  668.     fprintf(stderr,"line %d: unmatched end for environment ``%s''\n",line,
  669.         env[j].env_name);
  670.                     env[j].env_end--;    /* reset */
  671.                     }
  672. /* Give warning about unclosed openings in these environments */
  673.                 if (strcmp(env[j].env_name,"equation") == 0 ||
  674.                     strcmp(env[j].env_name,"eqnarray") == 0 ||
  675.                     strcmp(env[j].env_name,"eqnarray*") == 0 ||
  676.                     strcmp(env[j].env_name,"displaymath") == 0)
  677.                     {
  678.                     if ((lbr-rbr) > 0)
  679.     fprintf(stderr,"line %d: %d unclosed braces in environment ``%s''\n",
  680. line,lbr-rbr,env[j].env_name);
  681.                     if ((lb-rb) > 0)
  682.     fprintf(stderr,"line %d: %d unclosed brackets in environment ``%s''\n",
  683. line,lb-rb,env[j].env_name);
  684.                     if ((lp-rp) > 0)
  685.     fprintf(stderr,"line %d: %d unclosed parentheses in environment ``%s''\n",
  686. line,lp-rp,env[j].env_name);
  687. /* clear registers */
  688.                     lp=0; lb=0; lbr=0;
  689.                     rp=0; rb=0; rbr=0;
  690.                     lpl=0; lbrl=0; lbl=0;
  691.                     }
  692.                 }
  693.                 }
  694.             else if (strcmp(w,"def") == 0)
  695.                 (void) def_file(fp,&line,1);
  696.             else if (strcmp(w,"newcommand") == 0)
  697.                 (void) comm_file(fp,&line,1);
  698.             else if (strcmp(w,"newenvironment") == 0)
  699.                 (void) getenv_file(fp,&line,1);
  700.             else if (i > 0)        fseek(fp,-1,1);
  701.             break;
  702.         default:
  703.             break;
  704.         }
  705.     c1=c;                    /* update previous character */
  706.     if (ld == 1 && rd == 1)
  707.         {ld=0.;        rd=0.;}        /* matched dollar signs */
  708.     if (ldd == 1 && rdd == 1)
  709.         {ldd=0.;    rdd=0.;}    /* matched double dollar signs */
  710.     }
  711. if ((lbr-rbr) > 0)
  712.     fprintf(stderr,"file ends: %d unclosed left braces, first opened at line %d \n",lbr-rbr,lbrl);
  713. if ((lb-rb) > 0)
  714.     fprintf(stderr,"file ends: %d unclosed left brackets, first opened at line %d\n",lb-rb,lbl);
  715. if ((lp-rp) > 0)
  716.     fprintf(stderr,"file ends: %d unclosed left parentheses, first opened at line %d\n",lp-rp,lpl);
  717. if (ld == 1)
  718.     fprintf(stderr,"file ends: single dollar sign opened at line %d unclosed\n",ldl);
  719. if (ldd == 1)
  720.     fprintf(stderr,"file ends: double dollar sign opened at line %d unclosed\n",lddl);
  721. if (disp == 1)
  722.     fprintf(stderr,"file ends: displayed equation opened at line %d unclosed\n",disp_line);
  723. if (form == 1)
  724.     fprintf(stderr,"file ends: formula opened at line %d unclosed\n",lform);
  725. for (i=0; i<env_count; i++)
  726.     if (env[i].env_beg > env[i].env_end)
  727.     fprintf(stderr,"file ends: enviornment ``%s'' begun at line %d not ended\n",env[i].env_name,env[i].beg_line);
  728. }
  729. SHAR_EOF
  730. fi # end of overwriting check
  731. if test -f 'README'
  732. then
  733.     echo shar: will not over-write existing file "'README'"
  734. else
  735. cat << \SHAR_EOF > 'README'
  736. TeXTools    Version    1.0
  737. Date:        1/25/87
  738.  
  739. Copyright (C) 1987  by Kamal Al-Yahya
  740.  
  741. This directory contains some filters that were developed at the
  742. Stanford Exploration Project (SEP), Geophysics Department, by Kamal Al-Yahya.
  743. Copying them to any other machine is permitted without prior permission
  744. provided that copyright messages are kept, no profit is made by copying
  745. the files, and modifications are clearly documented and noted in the
  746. edit history below.
  747.  
  748. --------------------------------------------------------------------------
  749. EDIT HISTORY:
  750.  
  751. --------------------------------------------------------------------------
  752.  
  753. Acknowledgment:
  754. Many users at the SEP gave valuable feedbacks that improved the programs.
  755.  
  756. The main programs have names that end with either 1 or 2 (e.g. detex1.c,
  757. detex2.c). Those ending with 2 are used in makefile.par and can
  758. be used only by those who have getpar(). This enables them to use
  759. in=  and out=  for input and output files specifications in addition
  760. to what can be done in files ending with 1.
  761.  
  762. The maximum number of characters in a document is set by MAXLEN
  763. (in setups.h) to be 65535. If the limit of unsigned integers in
  764. your machine is lower than this, change this number accordingly.
  765.  
  766. To install:
  767.  - modify MAXLEN in setups.h if necessary.
  768.  - choose either makefile or makefile.par (as explained above).
  769.  - type 'make'.
  770.  - test the programs on the testfile provided.
  771.  - move the executables to a common path (like /usr/local/bin).
  772.  - type 'make clean'.
  773.  
  774. The following files should be in this directory:
  775.  
  776. README        this file
  777. setups.h    an include file used by all programs.
  778. testfile    a file that demonstrates how these programs work
  779. inc_file    an include file that is opened by testfile
  780. inc_file2.tex    another include file
  781. makefile    what else but a makefile
  782. makefile.par    a makefile that assumes you have access to getpar()
  783. detex1.c    strips TeX's commands from the document
  784. detex2.c    same as detex1.c but assumes you have access to getpar()
  785. texeqn1.c    picks displayed equations from a document
  786. texeqn2.c    same as texeqn1.c but assumes you have access to getpar()
  787. texexpand1.c    expands the document by opening \input and \include files
  788. texexpand2.c    same as texexpand1.c but assumes you have access to getpar()
  789. texmatch1.c    checks for matching braces, brackets, parentheses, and dollar signs
  790. texmatch2.c    same as texmatch1.c but assumes you have access to getpar()
  791. DeTeX.c        subroutine to strip TeX's commands from the document
  792. Eqn.c        subroutine to extracts equations
  793. Expand.c    subroutine to expand the document
  794. Match.c        subroutine that checks the matching
  795. subs.c        subroutines used by the programs
  796. TEX        a shell that can be used to run all TeX processing
  797. texspell    shell that runs spell on TeX documnets
  798. detex.1        manual page for detex
  799. texeqn.1    manual page for texeqn
  800. texexpand.1    manual page for texexpand
  801. texmatch.1    manual page for texmatch
  802. texspell.1    manual page for texspell
  803. TEX.1        manual page for TEX
  804.  
  805.  
  806. Feedbacks are welcome. E-mail: try any of these
  807.  
  808.         kamal@hanauma.stanford.edu
  809.         kamal%hanauma@score.stanford.edu
  810. SHAR_EOF
  811. fi # end of overwriting check
  812. if test -f 'TEX'
  813. then
  814.     echo shar: will not over-write existing file "'TEX'"
  815. else
  816. cat << \SHAR_EOF > 'TEX'
  817. #! /bin/csh -f
  818. #
  819. # Usage: TEX [-flags ...] filename
  820. #
  821. # The various flags are described below, but only one filename should
  822. # be given; stdin is not used. File types are indicated by the filename
  823. # suffix. Input files may have one of the following suffixes:
  824. #    .tex -- a file with tex commands, equations.
  825. #    .dvi -- device independent format.
  826. #    .ver -- output of verser1 (for the varian or AED)
  827. #    .imp -- output of dvi-imagen (or dviimp)
  828. # Anything else is assumed to be in .tex format.
  829. # If TEX sees a .dvi, .ver, or .imp suffix, it will skip ahead to the right
  830. # point in the processing sequence. Specifically,
  831. #    texeqn    accepts .tex, outputs .tex
  832. #    tex    accepts .tex, outputs .dvi and .log
  833. #    latex    accepts .tex, outputs .dvi and .log
  834. #    verser1    accepts .dvi, outputs .ver (for the varian or AED, not on hanuma).
  835. #    lpr    accepts .ver, outputs raster
  836. #    ipr    accepts    .imp, outputs raster
  837. #
  838. # Flags:
  839. # -latex uses LaTeX.
  840. # -log     saves a log file from the tex run in filename.log.
  841. # -d     quits once the .dvi file has been made.
  842. # -x     makes two passes on the (latex) input, so cross-references
  843. #     are resolved.
  844. # -v     output device is the varian (imagen is the default)
  845. # -q     quits once the .imp file has been made if the imagen is the target printer
  846. #     or after the .ver file    (i.e. after verser1 stage) if the AED or the varian
  847. #     is the target printer.
  848. # -eqn     strips out the equations with texeqn and typeset them.
  849. #
  850. # Authors: Kamal Al-Yahya, Jeff Thorson, and Chuck Sword, Stanfor University
  851. #
  852. umask 0
  853. onintr removal
  854. set name=() host=()
  855. set destdir = /usr/local
  856. set tmp = TEX$$
  857. set device = imagen
  858. set st = 0
  859. unset latex x d q eqn log
  860.  
  861. if ($#argv == 0) then
  862.     echo "usage: TEX [-latex] [-eqn] [-log] [-d] [-q] [-x] filename"
  863.     exit(-1)
  864. endif
  865.  
  866. while ($#argv > 0 && !($?inf))
  867.     switch ($argv[1])
  868.         case -latex:
  869.             set latex
  870.             breaksw
  871.  
  872.         case -x:
  873.             set x
  874.             breaksw
  875.  
  876.         case -q:
  877.             set q
  878.             breaksw
  879.  
  880.         case -d:
  881.             set d
  882.             breaksw
  883.  
  884.         case -v:
  885.             set device = varian
  886.             breaksw
  887.  
  888.         case -eqn:
  889.             set eqn
  890.             breaksw
  891.  
  892.         case -log:
  893.             set log
  894.             breaksw
  895.  
  896.         case -*:
  897.             echo unknown flag $argv[1], ignored
  898.             breaksw
  899.                 default:
  900.             set inf = $argv[1]
  901.             if !(-e $inf) then
  902.  
  903. #  filename not found, try with .tex ending
  904.  
  905.                 if !(-e $inf.tex) then 
  906.                     echo $0 'cannot find file' $inf.
  907.                     exit(-1)
  908.                 else
  909.                     set inf = ($inf.tex)
  910.                 endif
  911.             endif
  912.             breaksw
  913.         endsw
  914.     shift argv
  915. end
  916.  
  917. set name = $inf:t
  918. set sname = $name:r
  919. set name = $cwd/$name
  920. set suffix = $name:e
  921.  
  922. if ($suffix == dvi) then
  923.     echo TEX: starting with .dvi file
  924.     set name = $name:r
  925.     set dvifile = $inf
  926.     goto dvi
  927. endif
  928.  
  929. if ($suffix == ver) then
  930.     echo TEX: starting with .ver file
  931.     set name = $name:r
  932.     set verfile = $inf
  933.     goto ver
  934. endif
  935.  
  936. if ($suffix == imp) then
  937.     echo TEX: starting with .imp file
  938.     set name = $name:r
  939.     set impfile = $inf
  940.     goto imp
  941. endif
  942.  
  943. if ($suffix == tex || $suffix == eqn) then
  944.     set name = $name:r
  945. endif
  946.  
  947. echo "\batchmode" > $tmp.tex
  948.  
  949. if ($?eqn) then
  950.     $destdir/texeqn < $inf >> $tmp.tex
  951. else
  952.     cat $inf >> $tmp.tex
  953. endif
  954.  
  955. echo "\bye" >> $tmp.tex
  956.  
  957. # Choose tex or latex
  958.  
  959. if ($?latex) then
  960.     if (-e $name.aux) then
  961.         cp $name.aux $tmp.aux
  962.     endif
  963.     $destdir/latex $tmp:t
  964.     if ($status != 0) then
  965.         goto oops
  966.     else
  967.         if (-e $tmp.aux) then
  968.             cp $tmp.aux $name.aux
  969.         endif
  970.     endif
  971.  
  972.     if ($?x) then
  973.         echo "Starting second pass"
  974.         $destdir/latex $tmp
  975.         if ($status != 0) then
  976.             goto oops
  977.         endif
  978.         if (-e $tmp.aux) then
  979.             cp $tmp.aux $name.aux
  980.         endif
  981.     endif
  982.  
  983. else    $destdir/tex $tmp
  984.     if ($status != 0) then
  985. oops:
  986.         echo TEX could not process your file.
  987.         echo Error messages are in $name.log
  988.         mv -f $tmp.log $name.log
  989.         set st = -1
  990.         goto removal
  991.     endif
  992. endif
  993.  
  994. if ($?log) then
  995.     mv -f $tmp.log $name.log
  996.     if (-e $tmp.aux) then
  997.         mv -f $tmp.aux $name.aux
  998.     endif
  999. endif
  1000.  
  1001. set dvifile = $tmp.dvi
  1002.  
  1003. if ($?d) then
  1004.     mv -f $dvifile $name.dvi
  1005.     goto removal
  1006. endif
  1007.  
  1008. dvi:
  1009.  
  1010. if($device == imagen) then
  1011.     $destdir/dvi-imagen -s $dvifile > $tmp.imp
  1012.     if ($?q) then
  1013.         mv -f $tmp.imp $name.imp
  1014.         goto removal
  1015.     endif
  1016.     set impfile = $tmp.imp
  1017. imp:
  1018.     (echo -n \@document\(owner \"$user\", site \"$host\", spooldate \
  1019.     \"`date`\", language \"imPress\", jobheader off, \
  1020.     jamresistance on\) ; cat $impfile ) | $destdir/ipr
  1021.     goto removal
  1022. endif
  1023.  
  1024. if($device == varian) then
  1025.     $destdir/verser1 < $dvifile > $tmp.ver
  1026.     if ($status != 0) then
  1027.         echo TEX bombed out on verser1.
  1028.         set st = -1
  1029.         goto removal
  1030.     endif
  1031.     set verfile = $tmp.ver
  1032.  
  1033.     if ($?q) then
  1034.         mv -f $verfile $name.ver
  1035.         goto removal
  1036.     endif
  1037. ver:
  1038.     lpr -d -s -Pvarian $tmp.ver
  1039. endif
  1040.  
  1041. removal:
  1042. /bin/rm -f $tmp.tex $tmp.log $tmp.dvi $tmp.ver $tmp.imp $tmp.aux
  1043. exit($st)
  1044. SHAR_EOF
  1045. chmod +x 'TEX'
  1046. fi # end of overwriting check
  1047. if test -f 'TEX.1'
  1048. then
  1049.     echo shar: will not over-write existing file "'TEX.1'"
  1050. else
  1051. cat << \SHAR_EOF > 'TEX.1'
  1052. .TH TEX 1 2/2/84
  1053. .UC 4
  1054. .SH NAME
  1055. TEX \- 
  1056. .I TeX
  1057. and
  1058. .I LaTeX
  1059. typesetter and printer.
  1060. .SH SYNOPSIS
  1061. .B TEX
  1062. [
  1063. options
  1064. ]
  1065. .I  filename
  1066. .SH DESCRIPTION
  1067. .I TEX
  1068. processes a file 
  1069. and sends it to a printer. The default is the Imagen; other devices 
  1070. can be used if available at the site.
  1071. .PP
  1072. The input to
  1073. .I TEX
  1074. does not have to be the raw manuscript.
  1075. .I TEX
  1076. can be given a
  1077. .I .dvi
  1078. file (which results from using the
  1079. .B -d
  1080. option),
  1081. or
  1082. .I .imp
  1083. and
  1084. .I .ver
  1085. files (which result from using the
  1086. .B -q
  1087. option), and it will proceed from and end at the appropriate stage.
  1088. .br
  1089. The file name does not have to end with
  1090. .I
  1091. tex.
  1092. .sp 2
  1093. OPTIONS :
  1094. .br
  1095. .TP
  1096. .B \-latex
  1097. uses LaTeX.  See the LaTeX manual.
  1098. .TP
  1099. .B \-x
  1100. makes two passes on the
  1101. .I LaTeX
  1102. input to resolve cross-references.
  1103. .TP
  1104. .B \-eqn
  1105. uses
  1106. .B
  1107. texeqn
  1108. to extract the equations.
  1109. .TP
  1110. .B \-log
  1111. saves messages from the
  1112. .B tex
  1113. run in 
  1114. .I filename
  1115. .B .log
  1116. and saves messages from LaTeX run in 
  1117. .I filename
  1118. .B .aux
  1119. .TP
  1120. .B \-d
  1121. quits once the
  1122. .I dvi
  1123. file is produced without producing a hardcopy.
  1124. .TP
  1125. .B \-q
  1126. (for quiescent) intermediate output is not spooled 
  1127. to the printing device.
  1128. .TP
  1129. .B \-v
  1130. output device is the varian (default is the imagen).
  1131. .fi
  1132. .SH SEE ALSO
  1133. tex(1), texeqn(1), texmatch(1), detex(1)
  1134. .SH FILES
  1135. file.aux            the auxiliary file used by LaTeX for labeling figures.
  1136. .br
  1137. The default is to remove the following files at the end of the run:
  1138. .br
  1139. TE????.dvi        the device independent file.
  1140. .br
  1141. TE????.log        the log file.
  1142. .br
  1143. TE????.imp        the impress file; can be printed by
  1144. .B ipr
  1145. file.
  1146. .SH BUGS
  1147. Only one file can be processed at a time.
  1148. .br
  1149. Drivers flags are not incorporated. Add the ones you need.
  1150. .SH AUTHOR
  1151. Kamal Al-Yahya
  1152. .br
  1153. Jeff Thorson
  1154. .br
  1155. Chuck Sword
  1156. SHAR_EOF
  1157. fi # end of overwriting check
  1158. if test -f 'detex.1'
  1159. then
  1160.     echo shar: will not over-write existing file "'detex.1'"
  1161. else
  1162. cat << \SHAR_EOF > 'detex.1'
  1163. .TH detex 1 2/27/86
  1164. .UC 4
  1165. .SH NAME
  1166. detex \- a filter to strip TeX and LaTeX's commands from a file.
  1167. .SH SYNOPSIS
  1168. .B detex [-iw]
  1169. .I file1 [file2 ......]
  1170. .br
  1171. or
  1172. .B detex [-iw]
  1173. .I < file
  1174. .br
  1175. .SH DESCRIPTION
  1176. TeX and LaTeX have control characters that
  1177. .B spell
  1178. and other
  1179. .I troff
  1180. -dependent
  1181. processors (like diction) do not recognize.
  1182. .I Detex
  1183. works as a preprocessor by
  1184. filtering those control characters. The output can then be piped to the next
  1185. process. The output can be saved by redirecting the standard output.
  1186. .I Detex
  1187. does not break the document into individual words. It merely
  1188. .I erases
  1189. the control sequences.
  1190. .br
  1191. In-line or displayed equation are not  passed to the output. Also,
  1192. the character '%' is recognized as a comment indicator and the commented
  1193. text is not passed to the output.
  1194. .br
  1195. .I Detex
  1196. recognizes and opens files called by TeX's and LaTeX's \\input
  1197. and \\include commands. The
  1198. .B -i
  1199. flag makes
  1200. .I detex
  1201. ignore these commands.
  1202. The file name has to be correct relative to the current working directory.
  1203. If it cannot open the file nor file_name.tex, it will give a non-fatal
  1204. error message and proceed.
  1205. .br
  1206. Warning is given if suspected unmatching is detected. Use the
  1207. .B -w
  1208. flag to suppress these warnings.
  1209. .SH DIAGNOSTICS
  1210. Nesting of \\input and \\include is allowed but the number of opened files
  1211. must not exceed the system's limit on the number of simultaneously opened
  1212. files (normally < 20).
  1213. .br
  1214. Displayed material is regarded as mathematical equations and is ignored.
  1215. .br
  1216. White spaces withing LaTeX's \\begin{...} or \\end{...} are not allowed for.
  1217. .SH SEE ALSO
  1218. texexpand(1), texeqn(1), texmatch(1).
  1219. .SH AUTHOR
  1220. Kamal Al-Yahya, Stanford University
  1221. SHAR_EOF
  1222. fi # end of overwriting check
  1223. if test -f 'detex1.c'
  1224. then
  1225.     echo shar: will not over-write existing file "'detex1.c'"
  1226. else
  1227. cat << \SHAR_EOF > 'detex1.c'
  1228. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  1229. /* detex: strips TeX's and LaTeX's commands */
  1230.  
  1231. char *documentation[] = {
  1232. " SYNTAX",
  1233. "        detex [-i] file1 [file2 .....]",
  1234. "     or detex [-i]  < file1 [file2 ....]",
  1235. "",
  1236. "See the manual page for more details.",
  1237. "",
  1238. "        Flag:",
  1239. "             -i:     ignores TeX's and LaTeX's \input and \include commands",
  1240. "             -w:     matching is not checked",
  1241. "",
  1242. };
  1243.  
  1244. /* Author: Kamal Al-Yahya, Stanford University,        11/1/83 */
  1245. /* Last modified:                    1/25/87 */
  1246.  
  1247. int    doclength = { sizeof documentation/sizeof documentation[0] };
  1248.  
  1249. #include        "setups.h"
  1250.  
  1251. #ifdef tops20
  1252. #define TEMPFILE "texXXXXXX"
  1253. #else
  1254. #define TEMPFILE "/tmp/texXXXXXX"
  1255. #endif
  1256.  
  1257. #ifdef MSC
  1258. #else
  1259. struct sgttyb ttystat;
  1260. #endif
  1261.  
  1262. extern char *mktemp();
  1263. char scratch_file[MAXWORD];
  1264.  
  1265. int wflag;
  1266. int xargc;
  1267. char **xargv;
  1268.  
  1269. main(argc,argv)
  1270. int argc; 
  1271. char *argv[];
  1272. {
  1273. char *buf;
  1274. FILE *temp,*scr;
  1275. register char *cptr;
  1276. int piped_in;
  1277. int iflag,i;
  1278.  
  1279. if (((buf = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL))
  1280.     {
  1281.         fprintf(stderr,"detex: Cannot malloc() internal buffer space\n\
  1282. Need an array of %d characters\n",MAXLEN);
  1283.     exit(-1);
  1284.     }
  1285.  
  1286. /* If no arguments, and not in a pipeline, self document */
  1287. #ifdef MSC    /* MS-DOS cannot distinguish piped input from no input */
  1288. piped_in = (argc == 1);
  1289. #else
  1290. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  1291. #endif
  1292.  
  1293. if (argc == 1 && !piped_in)
  1294.     {
  1295.     for( i=0; i<doclength; i++)
  1296.         printf("%s\n",documentation[i]);
  1297.     exit (0);
  1298.     }
  1299.  
  1300. /* process option flags */
  1301. xargc = argc;
  1302. xargv = argv;
  1303. for (xargc--,xargv++; xargc; xargc--,xargv++)
  1304.     {
  1305.     cptr = *xargv; 
  1306.     if( *cptr=='-' )
  1307.         {
  1308.         while( *(++cptr))
  1309.             {
  1310.             switch( *cptr )
  1311.                 {
  1312.                 case 'i':
  1313.                     iflag=1;
  1314.                     break;
  1315.                 case 'w':
  1316.                     wflag=1;
  1317.                     break;
  1318.                 default:
  1319.                          fprintf(stderr,
  1320.                         "detex: unknown flag -%c\n",*cptr);
  1321.                     break;
  1322.                 }
  1323.             }
  1324.         }
  1325.     }
  1326.  
  1327. /* first process pipe input */
  1328. if(piped_in)
  1329.     {
  1330. /* need to buffer; can't seek in pipes */
  1331. /* make a temporary and volatile file in /tmp */
  1332.     strcpy(scratch_file,TEMPFILE);
  1333.     mktemp(scratch_file);
  1334.     if ((scr=fopen(scratch_file,"w")) == (FILE *)NULL)
  1335.         {
  1336.         fprintf(stderr,
  1337.             "detex: Cannot open scratch file [%s]\n",scratch_file);
  1338.         exit(-1);
  1339.         }
  1340.     scrbuf(stdin,scr);
  1341.     fclose(scr);
  1342.     scr=fopen(scratch_file,"r");
  1343.     unlink(scratch_file);
  1344.     if (wflag != 1)
  1345.         {
  1346.         fprintf(stderr,"Checking matching...\n");
  1347.         Match(scr);
  1348.         fseek(scr,0,0);
  1349.         }
  1350. /* either expand or buffer */
  1351.     if (iflag != 1)
  1352.         { Expand(scr,buf);    fclose(scr); }
  1353.     else
  1354.         { tmpbuf(scr,buf);    fclose(scr); }
  1355.     if (wflag != 1)
  1356.         fprintf(stderr,"Checking matching done\n\n");
  1357.     DeTeX(buf,stdout);
  1358.     fclose(scr);
  1359.     }
  1360.  
  1361. /* then process input line for arguments and assume they are input files */
  1362. xargc = argc;
  1363. xargv = argv;
  1364. for (xargc--,xargv++; xargc; xargc--,xargv++)
  1365.     {
  1366.     cptr = *xargv; 
  1367.     if( *cptr=='-' ) continue;        /* this is a flag */
  1368.     if((temp=fopen(cptr,"r")) != (FILE *)NULL)
  1369.         {
  1370.         if (wflag != 1)
  1371.             {
  1372.             fprintf(stderr,"Checking matching...\n");
  1373.             fprintf(stderr,"%s:\n",cptr);
  1374.             Match(temp);
  1375.             fprintf(stderr,"\n");
  1376.             fseek(temp,0,0);
  1377.             }
  1378. /* either expand or buffer */
  1379.         if (iflag != 1)
  1380.             { Expand(temp,buf);    fclose(temp); }
  1381.         else
  1382.             { tmpbuf(temp,buf);    fclose(temp); }
  1383.         if (wflag != 1)
  1384.             fprintf(stderr,"Checking matching done\n\n");
  1385.         DeTeX(buf,stdout);
  1386.         fclose(temp);
  1387.         }
  1388.     else
  1389.         fprintf(stderr,"detex: Cannot open %s\n",cptr);
  1390.     }
  1391.  
  1392. }
  1393. SHAR_EOF
  1394. fi # end of overwriting check
  1395. if test -f 'detex2.c'
  1396. then
  1397.     echo shar: will not over-write existing file "'detex2.c'"
  1398. else
  1399. cat << \SHAR_EOF > 'detex2.c'
  1400. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  1401. /* detex: strips TeX's and LaTeX's commands */
  1402.  
  1403.  
  1404. char *documentation[] = {
  1405. " SYNTAX",
  1406. "        detex [-iw] [parameters] [inputfiles]",
  1407. "",
  1408. "        flags:",
  1409. "              -i   ignores TeX's and LaTeX's \input and \include commands",
  1410. "              -w   does not check matching",
  1411. "",
  1412. "        parameters:",
  1413. "              in=filename       filename is the input file",
  1414. "                                (Default: in=stdin)",
  1415. "",
  1416. "              out=filename      filename is the output file",
  1417. "                                (Default: out=stdout)",
  1418. ""
  1419. };
  1420.  
  1421. /* Author: Kamal Al-Yahya, Stanford University,        11/1/83 */
  1422. /* Last modified:                    1/25/87 */
  1423.  
  1424. int    doclength = { sizeof documentation/sizeof documentation[0] };
  1425.  
  1426. #include        "setups.h"
  1427.  
  1428. #ifdef tops20
  1429. #define TEMPFILE "texXXXXXX"
  1430. #else
  1431. #define TEMPFILE "/tmp/texXXXXXX"
  1432. #endif
  1433.  
  1434. char string[MAXWORD], filename[MAXWORD], scratch_file[MAXWORD];
  1435. FILE *out_file;
  1436. extern char *mktemp();
  1437.  
  1438. #ifdef MSC
  1439. #else
  1440. struct sgttyb ttystat;
  1441. #endif
  1442.  
  1443. int wflag;
  1444. int xargc;
  1445. char **xargv;
  1446.  
  1447. main(argc,argv)
  1448. int argc; 
  1449. char *argv[];
  1450. {
  1451. char *buf;
  1452. FILE *temp,*scr;
  1453. register char *cptr;
  1454. int piped_in;
  1455. int iflag,i;
  1456.  
  1457. if (((buf = (char *)malloc(MAXLEN*sizeof(char))) == (char *)NULL))
  1458.     {
  1459.         fprintf(stderr,"detex: Cannot malloc() internal buffer space\n\
  1460. Need an array of %d characters\n",MAXLEN);
  1461.     exit(-1);
  1462.     }
  1463.  
  1464. /* If no arguments, and not in a pipeline, self document */
  1465. #ifdef MSC    /* MS-DOS cannot distinguish piped input from no input */
  1466. piped_in = (argc == 1);
  1467. #else
  1468. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  1469. #endif
  1470. if (argc == 1 && !piped_in)
  1471.     {
  1472.     for( i=0; i<doclength; i++)
  1473.         printf("%s\n",documentation[i]);
  1474.     exit (0);
  1475.     }
  1476.  
  1477. out_file = stdout;        /* default output */
  1478.  
  1479. /* process option flags */
  1480. xargc = argc;
  1481. xargv = argv;
  1482. for (xargc--,xargv++; xargc; xargc--,xargv++)
  1483.     {
  1484.     cptr = *xargv; 
  1485.     if( *cptr=='-' )
  1486.         {
  1487.         while( *(++cptr))
  1488.             {
  1489.             switch( *cptr )
  1490.                 {
  1491.                 case 'i':
  1492.                     iflag=1;
  1493.                     break;
  1494.                 case 'w':
  1495.                     wflag=1;
  1496.                     break;
  1497.                 default:
  1498.                          fprintf(stderr,
  1499.                         "detex: unknown flag -%c\n",*cptr);
  1500.                     break;
  1501.                 }
  1502.             }
  1503.         }
  1504.     }
  1505.  
  1506. /* process getpar parameters */
  1507. xargc = argc;
  1508. xargv = argv;
  1509.  
  1510. if(getpar_("out","s",string))
  1511.     {
  1512.     sscanf(string,"%s",filename);
  1513.     if((temp=fopen(filename,"w")) == NULL)
  1514.         fprintf(stderr,"detex: Cannot open output file %s\n",filename);
  1515.     else
  1516.         out_file = temp;
  1517.     }
  1518.  
  1519. /* first process pipe input */
  1520. if(piped_in)
  1521.     {
  1522. /* need to buffer; can't seek in pipes */
  1523. /* make a temporary and volatile file in /tmp */
  1524.     strcpy(scratch_file,TEMPFILE);
  1525.     mktemp(scratch_file);
  1526.     if ((scr=fopen(scratch_file,"w")) == (FILE *)NULL)
  1527.         {
  1528.         fprintf(stderr,
  1529.             "detex: Cannot open scratch file [%s]\n",scratch_file);
  1530.         exit(-1);
  1531.         }
  1532.     scrbuf(stdin,scr);
  1533.     fclose(scr);
  1534.     scr=fopen(scratch_file,"r");
  1535.     unlink(scratch_file);
  1536.     if (wflag != 1)
  1537.         {
  1538.         fprintf(stderr,"Checking matching...\n");
  1539.         Match(scr);
  1540.         fseek(scr,0,0);
  1541.         }
  1542. /* either expand or buffer */
  1543.     if (iflag != 1)
  1544.         { Expand(scr,buf);    fclose(scr); }
  1545.     else
  1546.         { tmpbuf(scr,buf);    fclose(scr); }
  1547.     if (wflag != 1)
  1548.         fprintf(stderr,"Checking matching done\n\n");
  1549.     DeTeX(buf,out_file);
  1550.     fclose(scr);
  1551.     }
  1552.  
  1553. /* next process in=inputfiles */
  1554. if(getpar_("in","s",string))
  1555.     {
  1556.     sscanf(string,"%s",filename);
  1557.     if((temp=fopen(filename,"r")) != NULL)
  1558.         {
  1559.         if (wflag != 1)
  1560.             {
  1561.             fprintf(stderr,"Checking matching...\n");
  1562.             fprintf(stderr,"%s:\n",filename);
  1563.             Match(temp);
  1564.             fprintf(stderr,"\n");
  1565.             fseek(temp,0,0);
  1566.             }
  1567. /* either expand or buffer */
  1568.         if (iflag != 1)
  1569.             { Expand(temp,buf);    fclose(temp); }
  1570.         else
  1571.             { tmpbuf(temp,buf);    fclose(temp); }
  1572.         if (wflag != 1)
  1573.             fprintf(stderr,"Checking matching done\n\n");
  1574.         DeTeX(buf,out_file);
  1575.         fclose(temp);
  1576.         }
  1577.     else
  1578.         fprintf(stderr,"detex: Cannot open %s\n",filename);
  1579.     }
  1580.  
  1581. /* then process input line for arguments and assume they are input files */
  1582. xargc = argc;
  1583. xargv = argv;
  1584.  
  1585. for (xargc--,xargv++; xargc; xargc--,xargv++)
  1586.     {
  1587.     cptr = *xargv; 
  1588.     if( *cptr=='-' ) continue; /* this is a flag */
  1589.     while (*cptr)
  1590.         {
  1591.         if (*cptr == '=')  break; /* this is for getpar */
  1592.         cptr++;
  1593.         }       
  1594.     if (*cptr)  continue;
  1595.     cptr = *xargv;
  1596.     if((temp=fopen(cptr,"r")) != (FILE *)NULL)
  1597.         {
  1598.         if (wflag != 1)
  1599.             {
  1600.             fprintf(stderr,"Checking matching...\n");
  1601.             fprintf(stderr,"%s:\n",cptr);
  1602.             Match(temp);
  1603.             fprintf(stderr,"\n");
  1604.             fseek(temp,0,0);
  1605.             }
  1606. /* either expand or buffer */
  1607.         if (iflag != 1)
  1608.             { Expand(temp,buf);    fclose(temp); }
  1609.         else
  1610.             { tmpbuf(temp,buf);    fclose(temp); }
  1611.         if (wflag != 1)
  1612.             fprintf(stderr,"Checking matching done\n\n");
  1613.         DeTeX(buf,out_file);
  1614.         fclose(temp);
  1615.         }
  1616.     else
  1617.         fprintf(stderr,"detex: Cannot open %s\n",cptr);
  1618.     }
  1619.  
  1620. }
  1621. SHAR_EOF
  1622. fi # end of overwriting check
  1623. if test -f 'inc_file'
  1624. then
  1625.     echo shar: will not over-write existing file "'inc_file'"
  1626. else
  1627. cat << \SHAR_EOF > 'inc_file'
  1628. This is an included file.
  1629. when texexpand is run on testfile, this file should be inserted.
  1630. (unmatching in inc_file))
  1631. END OF INSERTED FILE.
  1632. SHAR_EOF
  1633. fi # end of overwriting check
  1634. if test -f 'inc_file2.tex'
  1635. then
  1636.     echo shar: will not over-write existing file "'inc_file2.tex'"
  1637. else
  1638. cat << \SHAR_EOF > 'inc_file2.tex'
  1639. This is another included file
  1640. SHAR_EOF
  1641. fi # end of overwriting check
  1642. if test -f 'makefile'
  1643. then
  1644.     echo shar: will not over-write existing file "'makefile'"
  1645. else
  1646. cat << \SHAR_EOF > 'makefile'
  1647. CFLAGS = -O
  1648. LINTFLAGS = -abchnpux
  1649. CSUBS = Expand.c Match.c subs.c
  1650. OSUBS = Expand.o Match.o subs.o
  1651. B =
  1652.  
  1653. default: all
  1654.  
  1655. all: texexpand detex texeqn texmatch
  1656.  
  1657. texexpand: texexpand1.o $(OSUBS)
  1658.     cc $(CFLAGS) -o $(B)texexpand texexpand1.o $(OSUBS)
  1659.  
  1660. detex: detex1.o DeTeX.o $(OSUBS)
  1661.     cc $(CFLAGS) -o $(B)detex detex1.o DeTeX.o $(OSUBS)
  1662.  
  1663. texeqn: texeqn1.o Eqn.o $(OSUBS)
  1664.     cc $(CFLAGS) -o $(B)texeqn texeqn1.o Eqn.o $(OSUBS)
  1665.  
  1666. texmatch: texmatch1.o $(OSUBS)
  1667.     cc $(CFLAGS) -o $(B)texmatch texmatch1.o $(OSUBS)
  1668.  
  1669. lint:
  1670.     lint $(LINTFLAGS) texexpand1.c $(CSUBS) > texexpand.lnt
  1671.     lint $(LINTFLAGS) detex1.c DeTeX.c $(CSUBS) > detex.lnt
  1672.     lint $(LINTFLAGS) texeqn1.c Eqn.c $(CSUBS) > texeqn.lnt
  1673.     lint $(LINTFLAGS) texmatch1.c $(CSUBS) > texmatch.lnt
  1674.     
  1675. clean:
  1676.     /bin/rm -f *.o texexpand detex texeqn texmatch core *junk*
  1677. SHAR_EOF
  1678. fi # end of overwriting check
  1679. if test -f 'makefile.msc'
  1680. then
  1681.     echo shar: will not over-write existing file "'makefile.msc'"
  1682. else
  1683. cat << \SHAR_EOF > 'makefile.msc'
  1684. #-----------------------------------------------------------------------
  1685. # Makefile for textools
  1686. # Make targets:
  1687. #    (none)    same as all
  1688. #    all    produces all executables
  1689. #    lint    run lint on sources
  1690. #    clean    remove object files
  1691. #    share    make ../textools.sh for mailing
  1692. #
  1693.  
  1694. CFLAGS = -O
  1695. LINTFLAGS = -abchnpux
  1696. CSUBS = Expand.c Match.c subs.c
  1697. OSUBS = Expand.o Match.o subs.o
  1698. B =
  1699.  
  1700. default: all
  1701.  
  1702. all: texexpand detex texeqn texmatch
  1703.  
  1704. texexpand: texexpand1.o $(OSUBS)
  1705.     cc $(CFLAGS) -o $(B)texexpand texexpand1.o $(OSUBS)
  1706.  
  1707. detex: detex1.o DeTeX.o $(OSUBS)
  1708.     cc $(CFLAGS) -o $(B)detex detex1.o DeTeX.o $(OSUBS)
  1709.  
  1710. texeqn: texeqn1.o Eqn.o $(OSUBS)
  1711.     cc $(CFLAGS) -o $(B)texeqn texeqn1.o Eqn.o $(OSUBS)
  1712.  
  1713. texmatch: texmatch1.o $(OSUBS)
  1714.     cc $(CFLAGS) -o $(B)texmatch texmatch1.o $(OSUBS)
  1715.  
  1716. share:
  1717.     make clean
  1718.     makescript ../textools.sh *
  1719.  
  1720. lint:
  1721.     lint $(LINTFLAGS) texexpand1.c $(CSUBS) > texexpand.lnt
  1722.     lint $(LINTFLAGS) detex1.c DeTeX.c $(CSUBS) > detex.lnt
  1723.     lint $(LINTFLAGS) texeqn1.c Eqn.c $(CSUBS) > texeqn.lnt
  1724.     lint $(LINTFLAGS) texmatch1.c $(CSUBS) > texmatch.lnt
  1725.     
  1726. clean:
  1727.     \rm -f *.o core *junk* lint.lst
  1728. SHAR_EOF
  1729. fi # end of overwriting check
  1730. if test -f 'makefile.par'
  1731. then
  1732.     echo shar: will not over-write existing file "'makefile.par'"
  1733. else
  1734. cat << \SHAR_EOF > 'makefile.par'
  1735. # Use this makefile if you have access to getpar(). You then
  1736. # need to link with the library that provides it (e.g. -lsep below).
  1737. # If you don't use the other makefile (./makefiile).
  1738.  
  1739. CFLAGS = -O
  1740. LINTFLAGS = -abchnpux
  1741. CSUBS = Expand.c Match.c subs.c
  1742. OSUBS = Expand.o Match.o subs.o
  1743. B =
  1744.  
  1745. default: all
  1746.  
  1747. all: texexpand detex texeqn texmatch
  1748.  
  1749. texexpand: texexpand2.o $(OSUBS)
  1750.     cc $(CFLAGS) -o $(B)texexpand texexpand2.o $(OSUBS) -lsep
  1751.  
  1752. detex: detex2.o DeTeX.o $(OSUBS)
  1753.     cc $(CFLAGS) -o $(B)detex detex2.o DeTeX.o $(OSUBS) -lsep
  1754.  
  1755. texeqn: texeqn2.o Eqn.o $(OSUBS)
  1756.     cc $(CFLAGS) -o $(B)texeqn texeqn2.o Eqn.o $(OSUBS) -lsep
  1757.  
  1758. texmatch: texmatch2.o $(OSUBS)
  1759.     cc $(CFLAGS) -o $(B)texmatch texmatch2.o $(OSUBS) -lsep
  1760.  
  1761. lint:
  1762.     lint $(LINTFLAGS) texexpand2.c $(CSUBS) > texexpand.lnt
  1763.     lint $(LINTFLAGS) detex2.c DeTeX.c $(CSUBS) > detex.lnt
  1764.     lint $(LINTFLAGS) texeqn2.c Eqn.c $(CSUBS) > texeqn.lnt
  1765.     lint $(LINTFLAGS) texmatch2.c $(CSUBS) > texmatch.lnt
  1766.     
  1767. clean:
  1768.     /bin/rm -f *.o core *junk* lint.lst
  1769. SHAR_EOF
  1770. fi # end of overwriting check
  1771. if test -f 'setups.h'
  1772. then
  1773.     echo shar: will not over-write existing file "'setups.h'"
  1774. else
  1775. cat << \SHAR_EOF > 'setups.h'
  1776. /* setup file */
  1777.  
  1778. #include        <stdio.h>
  1779. #ifdef MSC
  1780. #include        <string.h>
  1781. #include    <stdlib.h>    /* for type declarations */
  1782. #include    <io.h>        /* for type declarations */
  1783. #else
  1784. #include        <strings.h>
  1785. #include        <sys/ioctl.h>
  1786. #include        <sgtty.h>
  1787. #endif
  1788.  
  1789. #define MAXLEN    65535        /* maximum number of chars in a document */
  1790. #define     MAXWORD    100    /* maximum word length */
  1791. #define     MAXLINE    250    /* maximum line length */
  1792. #define     MAXENV    50    /* maximum number of environments */
  1793.  
  1794. extern char *malloc();
  1795.  
  1796. #ifdef IN_TM        /* can only declare globals once */
  1797. #else
  1798. extern
  1799. #endif
  1800. struct environment {
  1801.     char *env_name;        /* name of environment */
  1802.     int env_beg;        /* counter for \begin{environment} */
  1803.     int env_end;        /* counter for \end{environment} */
  1804.     int beg_line;        /* line number for \beging{environment} */
  1805.     } env[MAXENV];
  1806.  
  1807. #ifdef ANSI
  1808. int    begin_to_end(char*,char*);
  1809. int    command(char*,char*);
  1810. int    comm_file(FILE*,int*);
  1811. int    comment(char*);
  1812. int    def(char*,char*);
  1813. int    def_file(FILE*,int*);
  1814. int    display(char*);
  1815. int    dollar(char*,FILE*);
  1816. int    formula(char*);
  1817. int    get_buf_word(char*,char*);
  1818. int    getenv_file(FILE*,int*);
  1819. int    get_file_word(FILE*,char*,int*,int*);
  1820. int    is_new_env(char*,int);
  1821. int    one_dollar(char*);
  1822. void    scrbuf(FILE*,FILE*);
  1823. void    tmpbuf(FILE*,char*);
  1824. int    two_dollars(char*,FILE*);
  1825. #else
  1826. int    begin_to_end();
  1827. int    command();
  1828. int    comm_file();
  1829. int    comment();
  1830. int    def();
  1831. int    def_file();
  1832. int    display();
  1833. int    dollar();
  1834. int    formula();
  1835. int    get_buf_word();
  1836. int    getenv_file();
  1837. int    get_file_word();
  1838. int    is_new_env();
  1839. int    one_dollar();
  1840. void    scrbuf();
  1841. void    tmpbuf();
  1842. int    two_dollars();
  1843. #endif
  1844. SHAR_EOF
  1845. fi # end of overwriting check
  1846. ##############################   CONNECT HERE   #############################
  1847.  
  1848.